<?php

MYTCONN();
$GETOPN[0] = 0;
$OpChannel = '';
$MonDPago = '';
if (isset($_POST['Of_Rate2']) && $_POST['Of_Rate2'] > 0) {

    $CNeg = $_POST['Of_Rate1'];
    $CLocal = $_POST['Of_Rate2'];
    if ($CNeg!=0) {$_POST['Of_Rate2'] = $CNeg*$CLocal;}
    if ($_POST['SetChannel'] > 0) {
        $OpChannel = ' AND Op_Modo = ' . $_POST['SetChannel'];
    }
    if ($_POST['MonDPago'] > 0) {
        $MonDPago = ' AND Op_MonDPago = ' . $_POST['MonDPago'];
    }

    $GETOPN = $GLOBALS['MYT']->db_array("SELECT Op_Orden, Of_Orden, Of_Rate2 FROM ordenespago
		LEFT OUTER JOIN ordenesfinanceiro ON Op_Orden = Of_Orden
		WHERE DATE(Op_Fecha) = DATE('{$_POST['SetTime']}') AND Op_Corr = {$_POST['QCorresponsal']} $OpChannel $MonDPago");
} elseif (isset($_GET['SetOf'])) {

    $CNeg = 1;
    $CLocal = $_GET['RTE'];
    $_POST['Of_Rate2'] = $CNeg*$CLocal;
    $_POST['QCorresponsal'] = $_GET['COR'];

    $GETOPN = $GLOBALS['MYT']->db_array("SELECT Op_Orden, Of_Orden, Of_Rate2 FROM ordenespago
		LEFT OUTER JOIN ordenesfinanceiro ON Op_Orden = Of_Orden
		WHERE Op_Orden = {$_GET['SetOf']}");
} else {
    echo 'Introduza uma taxa válida.';
}
for ($r = 1; $r <= $GETOPN[0]; $r++) {
    if (isset($GETOPN[$r]['Of_Orden'])) {
        if ($GLOBALS['MYT']->db_query("UPDATE ordenesfinanceiro SET Of_Rate2 = '{$_POST['Of_Rate2']}', Rate2User = {$_SESSION[$SESSID]}, Rate2Time = '{$sysTime}',Of_Rate3 = '{$_POST['Of_Rate2']}', Rate3User = {$_SESSION[$SESSID]}, Rate3Time = '{$sysTime}', Of_RateNeg = '{$CNeg}', Of_RateLocal = '{$CLocal}' WHERE Of_Orden = {$GETOPN[$r]['Of_Orden']}") == true) {
            echo '<script charset="utf-8" type="text/javascript">alert(\'Alterada taxa do lote ' . $GETOPN[$r]['Of_Orden'] . ' de ' . $GETOPN[$r]['Of_Rate2'] . ' para ' . $_POST['Of_Rate2'] . '\');</script>';
        } else {
            echo 'Erro a alterar.';
        }
    } else {
        $getCorrValues = $GLOBALS['MYT']->db_array("SELECT CorrCambio, CorrBeneficio FROM corresponsales WHERE CorrNumero = {$_POST['QCorresponsal']}");
        $GLOBALS['MYT']->db_query("INSERT INTO ordenesfinanceiro (Of_Orden, Of_Rate1, Of_Rate2, Of_Rate3, Mon1, Mon2, Iso1, Iso2, Rate2Time, Rate3Time, Rate2User, Rate3User, CorrRate, CorrFixed, Of_RateNeg, Of_RateLocal) VALUES ({$GETOPN[$r]['Op_Orden']},'0','{$_POST['Of_Rate2']}','{$_POST['Of_Rate2']}',0,0,'','','{$sysTime}','{$sysTime}',{$_SESSION[$SESSID]},{$_SESSION[$SESSID]},{$getCorrValues[1]['CorrCambio']},{$getCorrValues[1]['CorrBeneficio']},$CNeg,$CLocal)");
    }
}
if (!isset($_GET['SetOf'])) {
    echo '<div class="Confirm">Alteradas ' . $GETOPN[0] . ' ordens de pagamento</div>';
    mn_field('button', 'ED', 'Retry', 'Voltar', '', '', '', '" style="margin:4px" onclick="
				replaceUrl(\'RatesAction\',\'Modules/FTK9D4D0/Lib/a07_FCHHPANL.mex\');');
}